﻿using System;
using RomanJendrusz.Amazon.ETL.Infrastructure.Loging.Core;

namespace RomanJendrusz.Amazon.ETL.Infrastructure.DataBase
{
    public class DataBaseManager
    {
        private readonly IDataBaseContext dbContext;

        public DataBaseManager(IDataBaseContext dbContext)
        {
            this.dbContext = dbContext;
        }

        public void CreateDatabase()
        {
            if(dbContext.DataBaseExists)
            {
                throw new InvalidOperationException("The database allready exists");
            }
            LogGateway.LogFor(GetType()).Warn("Creating database....");
            dbContext.CreateDatabase();
            LogGateway.LogFor(GetType()).Warn("Database was created....");
        }

        public bool DatabaseExists
        {
            get { return dbContext.DataBaseExists;}
        }

        public void DropDatabase()
        {
            if (dbContext.DataBaseExists)
            {
                LogGateway.LogFor(GetType()).Warn("Droping database....");
                dbContext.DeleteDatabase();
                LogGateway.LogFor(GetType()).Warn("Database was droped....");
            }
        }

        public void RebuildDatabase()
        {
            LogGateway.LogFor(GetType()).Warn("Rebuilding database....");
            if(dbContext.DataBaseExists)
            {
                dbContext.DeleteDatabase();
            }
            dbContext.CreateDatabase();
            LogGateway.LogFor(GetType()).Warn("Database was rebuilded....");
        }
    }
}
